import base64 from '@/utils/base64'

const cll = localStorage.getItem('chatListLocal')

// initial state
const state = {
  currentCircleNav: 'allCircle', // 工具大全当前的页面 默认是微信好友请求
  circleData: {}, // 要重发的信息
  chatList: cll ? JSON.parse(cll) : [], // 待发送的信息
  circle2send: {}, // 待发送的朋友圈
  editMode: false // 是否是编辑模式
}

// getters
const getters = {
  currentCircleNav: (state) => state.currentCircleNav,
  circleData: (state) => state.circleData,
  chatList: (state) => state.chatList,
  chatListMap: (state) => {
    return state.chatList.map((x) => {
      return x.MsgSvrId
    })
  },
  circle2send: (state) => state.circle2send,
  editMode: (state) => state.editMode
}

// actions
const actions = {}

// mutations
const mutations = {
  // 设置tools当前的页面
  SET_CURRENT_CIRLCLE_NAV: (state, nav) => {
    state.currentCircleNav = nav
  },
  // 设置要重发的信息
  SET_CIRCLE_DATA: (state, data) => {
    state.circleData = data
  },
  // 设置要快速发朋友圈的信息
  SET_CHAT_LIST: (state, chatItem) => {
    const { MsgSvrId } = chatItem
    if (!MsgSvrId) return
    state.chatList.push(chatItem)
  },
  // 移除要快速发朋友圈的信息
  REMOVE_CHAT_LIST: (state, chatIndex) => {
    state.chatList.splice(chatIndex, 1)
  },
  // 更新大图信息
  UPDATE_CHAT_LIST: (state, bigImage) => {
    const { Content, MsgId } = bigImage
    let oc = {
      Url: base64.decode(Content)
    }
    const fi = state.chatList.findIndex((x) => {
      return x.MsgId === MsgId
    })
    if (fi >= 0) {
      oc = { ...state.chatList[fi], ...oc }
      state.chatList.splice(fi, 1, oc)
    }
  },
  // 删除待发朋友圈
  DELETE_CHAT_LIST_BY_ID: (state, chatId) => {
    console.log("DELETE_CHAT_LIST_BY_ID_1",state.chatList,chatId);
    state.chatList = state.chatList.filter((x) => {
      return chatId !== x.WeChatId + x.FriendId
    })
  },
  SET_CIRCLE_2_SEND: (state, circleItem) => {
    state.circle2send = circleItem
  },
  SET_EDIT_MODE: (state, editMode) => {
    state.editMode = editMode
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
